package com.fbinggun.fbinggunutil.utilclass;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Wolf;

/**
 * 坐标 常用与因world未加载导致Location坐标调用失败的替代坐标类
 */
public class Loc {

    Double x;
    Double y;
    Double z;
    float yaw;
    float pitch;
    String worldName;


    public Loc(Double x, Double y, Double z, float yaw, float pitch, String worldName) {
        this.x = x;
        this.y = y;
        this.z = z;
        this.yaw = yaw;
        this.pitch = pitch;
        this.worldName = worldName;
        if(this.worldName==null){
            this.worldName="zc";
        }
    }

    public Loc(Location location){
        this.x = location.getX();
        this.y = location.getY();
        this.z = location.getZ();
        this.yaw = location.getYaw();
        this.pitch = location.getPitch();
        this.worldName = location.getWorld().getName();
        if(this.worldName==null){
            this.worldName="zc";
        }
    }

    public Double getX() {
        return x;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public Double getY() {
        return y;
    }

    public void setY(Double y) {
        this.y = y;
    }

    public Double getZ() {
        return z;
    }

    public void setZ(Double z) {
        this.z = z;
    }

    public float getYaw() {
        return yaw;
    }

    public void setYaw(float yaw) {
        this.yaw = yaw;
    }

    public float getPitch() {
        return pitch;
    }

    public void setPitch(float pitch) {
        this.pitch = pitch;
    }

    public String getWorldName() {
        return worldName;
    }

    public void setWorldName(String worldName) {
        this.worldName = worldName;
    }

    public static Loc getLocation(String str) {
        String[] strings = str.split(",");
        String world = strings[0];
        double x = Double.valueOf(strings[1]);
        double y = Double.valueOf(strings[2]);
        double z = Double.valueOf(strings[3]);
        float yew = 0.0F;
        float pitch = 0.0F;
        if (strings.length == 6) {
            yew = Float.valueOf(strings[4]);
            pitch = Float.valueOf(strings[5]);
        }
        return new Loc(x, y, z, yew, pitch,world);
    }

    public static String toLocation(Loc loc) {
        String world = loc.getWorldName();
        return world + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getYaw() + "," + loc.getPitch();
    }


    public Location getLocation(){
        World world = Bukkit.getWorld(worldName);
        if(world==null){
            return null ;
        }
        return  new Location(world,x,y,z,yaw,pitch );
    }
    public Location getLocation( World world){
        if(world==null){
            return null ;
        }
        return  new Location(world,x,y,z,yaw,pitch );
    }


}
